package com.skwen.delivery.superdelivery.login.loginpresenter;

import android.os.Handler;

import com.skwen.delivery.superdelivery.login.bean.User;
import com.skwen.delivery.superdelivery.login.loginImpInterface.UserBisImp;
import com.skwen.delivery.superdelivery.login.loginInterface.LoginListener;
import com.skwen.delivery.superdelivery.login.loginInterface.UserBis;
import com.skwen.delivery.superdelivery.login.loginInterface.UserLoginView;

/**
 * Created by yundantian on 2017/8/28.
 */

public class UserLoginPresenter {


    private UserLoginView userLoginView;

    private UserBis userBis;

    private Handler mHandler = new Handler();


    public UserLoginPresenter(UserLoginView userLoginView) {
        this.userLoginView = userLoginView;
        userBis = new UserBisImp();
    }


    public void Login() {
        userLoginView.showsLoading();
        userBis.login(userLoginView.getUserName().trim(), userLoginView.getPassword().trim(), new LoginListener() {
            @Override
            public void loginSucess(User user) {
                mHandler.post(() -> {
                    userLoginView.toMainActivity(user);
                    userLoginView.hideLoading();
                });
            }

            @Override
            public void loginFaile(String error) {
                mHandler.post(() -> {
                    userLoginView.showFailedError(error);
                    userLoginView.hideLoading();
                });
            }
        });
    }


    public void openRegister(){
        userLoginView.toRegister();
    }

    public void register(User user) {
        userLoginView.showsLoading();
        userBis.register(user, new LoginListener() {
            @Override
            public void loginSucess(User user) {
                userLoginView.register(user);
                userLoginView.hideLoading();
            }

            @Override
            public void loginFaile(String error) {
                userLoginView.hideLoading();
            }
        });
    }
}
